﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class HTML_tool : System.Web.UI.Page
{
    string[] keys;
    protected void Page_Load(object sender, EventArgs e)
    {
         keys = Request.QueryString.AllKeys;
        //规范：1.operate 2.table 3.id
        
        if (Request.QueryString[keys[0]].ToString().Trim() == "del")
        {
            //删除用户
            DelUserInfo();
        }
        else if (Request.QueryString[keys[0]].ToString().Trim() == "add")
        {
            //添加用户
            AddUserInfo();
        }
        else if (Request.QueryString[keys[0]].ToString().Trim() == "update")
        {
            //修改用户
            UpdateUserInfo();

        }




        Response.End();
    }

    public void DelUserInfo()
    {
        if (Request.QueryString[keys[1]].ToString().Trim() == "tb_student")
        {
            string student_id = Request.QueryString[keys[2]];
            string sql = "delete from tb_student where student_id ='"+ student_id+ "'";
            int i =SqlServer.ExecuteNonQuery(sql);
            if (i > 0)
            {
                Response.Write(i.ToString());
            }
            else
            {
                Response.Write("0");//删除失败
            }
        }
        if (Request.QueryString[keys[1]].ToString().Trim() == "tb_teacher")
        {
            string teacher_id = Request.QueryString[keys[2]];
            string sql = "delete from tb_teacher where teacher_id ='" + teacher_id + "'";
            int i = SqlServer.ExecuteNonQuery(sql);
            if (i > 0)
            {
                Response.Write(i.ToString());
            }
            else
            {
                Response.Write("0");//删除失败
            }
        }
    }
    public void UpdateUserInfo()
    {
        if (Request.QueryString[keys[1]].ToString().Trim() == "tb_student")
        {
            List<string> set = new List<string>();
            for (int i = 3; i < keys.Length; i++)
            {
                if (i == 4 || i == 5) continue;//剔除学院和专业
                set.Add(String.Format(" {0} = '{1}' ", keys[i], Request.QueryString[keys[i]]));
            }
            string s = string.Join(" , ", set);
            string sql = "update  tb_student set "+ s
                + ", major_id = (select major_id from tb_major where " + String.Format(" {0} = '{1}' ", keys[5], Request.QueryString[keys[5]]) + ")"
                +" where student_id = '"+ Request.QueryString[keys[2]]+"'";
            int k = SqlServer.ExecuteNonQuery(sql);
            if (k > 0)
            {
                Response.Write(k.ToString());
            }
            else
            {
                Response.Write("0");//删除失败
            }
        }
        if (Request.QueryString[keys[1]].ToString().Trim() == "tb_teacher")
        {
            List<string> set = new List<string>();
            for (int i = 3; i < keys.Length; i++)
            {
                
                set.Add(String.Format(" {0} = '{1}' ", keys[i], Request.QueryString[keys[i]]));
            }
            string s = string.Join(" , ", set);
            string sql = "update  tb_teacher set " + s
                //+" , college_id = (select college_id from tb_college where college_name = '"+Request.QueryString[keys[4]]+"')"
                + " where teacher_id = '" + Request.QueryString[keys[2]] + "'";
            int k = SqlServer.ExecuteNonQuery(sql);
            if (k > 0)
            {
                Response.Write(k.ToString());
            }
            else
            {
                Response.Write("0");//删除失败
            }
        }
    }
    public void AddUserInfo()
    {
        #region
        if (Request.QueryString[keys[1]].ToString().Trim() == "tb_student")
        {
            //判断该学号是否存在学号
            string sqlID= "select count(*) from  tb_student where student_id = '"+Request.QueryString[keys[2]]+"'";
            if (Int32.Parse( SqlServer.ExecuteScalar(sqlID).ToString())>0)
            {
                Response.Write("-1");
                return;
            }
                
            string sql = "INSERT INTO [dbo].[tb_student]" +
                        "           ([student_id]" +
                        "           ,[student_pwd]" +
                        "           ,[student_name]" +
                        "           ,[student_tel]" +
                        "           ,[student_id_card]" +
                        "           ,[student_sex]" +
                        "           ,[student_photo_path]" +
                        "           ,[major_id])" +
                        "     select " +
                        String.Format("'{0}',", Request.QueryString[keys[2]]) +
                        String.Format("'{0}',", Request.QueryString[keys[3]]) +
                        String.Format("'{0}',", Request.QueryString[keys[4]]) +
                        String.Format("'{0}',", Request.QueryString[keys[5]]) +
                        String.Format("'{0}',", Request.QueryString[keys[9]]) +
                        String.Format("'{0}',", Request.QueryString[keys[8]]) +
                        String.Format("'{0}',", Request.QueryString[keys[10]]) +
                        " major_id from tb_major where major_name = '" + Request.QueryString[keys[6]] + "'";
            int k = SqlServer.ExecuteNonQuery(sql);
            if (k > 0)
            {
                Response.Write(k.ToString());
            }
            else
            {
                Response.Write("0");//删除失败
            }
        }
        #endregion

        #region
        if (Request.QueryString[keys[1]].ToString().Trim() == "tb_teacher")
        {
            //判断该学号是否存在账号
            string sqlID= "select count(*) from  tb_teacher where teacher_id = '"+Request.QueryString[keys[2]]+"'";
            if (Int32.Parse( SqlServer.ExecuteScalar(sqlID).ToString())>0)
            {
                Response.Write("-1");
                return;
            }
            
            string sql = "INSERT INTO [dbo].[tb_teacher]" +
                        "           ([teacher_id]" +
                        "           ,[teacher_pwd]" +
                        "           ,[teacher_name]" +
                        "           ,[teacher_tel]" +
                        "           ,[college_id] ) " +
                        "values(" +
                        String.Format("'{0}',", Request.QueryString[keys[2]]) +
                        String.Format("'{0}',", Request.QueryString[keys[3]]) +
                        String.Format("'{0}',", Request.QueryString[keys[4]]) +
                        String.Format("'{0}',", Request.QueryString[keys[5]]) +
                        String.Format("'{0}')", Request.QueryString[keys[6]]) ;
            int k = SqlServer.ExecuteNonQuery(sql);
            if (k > 0)
            {
                Response.Write(k.ToString());
            }
            else
            {
                Response.Write("0");//删除失败
            }
        }
        #endregion
    }
}